package server

import (
	"net/http"
	"open-platform/env"
	"open-platform/helper"
	"strings"
)

func Start() {

	mux := http.NewServeMux()

	mux.HandleFunc("/ticket", setTicket)
	mux.HandleFunc("/authorization", authorization)
	mux.HandleFunc("/authorized", authorized)
	mux.HandleFunc("/token/get", getToken)

	err := http.ListenAndServe(env.EnvConfig.ListenAddr, mux)

	helper.Handler(err, 1)

}

func response(w http.ResponseWriter, msg string, statusCode string) {

	if "" != statusCode {
		w.Header().Add("Status Code", statusCode)
	} else {
		w.Header().Add("Status Code", "200")
	}

	_, err := w.Write([]byte(msg))

	helper.Handler(err, 0)

}

func makeUrlParam(urlParam map[string]string) string {

	if urlParam == nil {
		return ""
	}

	url := ""

	for k, v := range urlParam {
		url += k + "=" + v + "&"
	}

	url = strings.TrimRight(url, "&")

	return url

}
